package com.example.cryptodemo.controller;

import com.example.cryptodemo.aes.AesException;
import com.example.cryptodemo.aes.BizMsgCrypt;
import com.example.cryptodemo.aes.BizMsgCryptUtil;
import com.example.cryptodemo.aes.Message;
import com.example.cryptodemo.dto.VerificationParametersDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 加解密处理器
 *
 * @author Yunkai Luo
 * @since 10:19 2021/11/5
 **/
@RestController
@RequestMapping("/crypto")
public class CryptoController {

    @Autowired BizMsgCrypt bizMsgCrypt;

    @GetMapping
    public String verify(
            @RequestParam String signature,
            @RequestParam String timeStamp,
            @RequestParam String nonce,
            @RequestParam String echoStr
    ) throws AesException {
        return BizMsgCryptUtil.verifyUrl(bizMsgCrypt,
                signature,
                timeStamp,
                nonce,
                echoStr);
    }

    @GetMapping("/another")
    public String verify(
            VerificationParametersDTO dto
    ) throws AesException {
        return BizMsgCryptUtil.verifyUrl(bizMsgCrypt,
                dto.getSignature(),
                dto.getTimeStamp(),
                dto.getNonce(),
                dto.getEchoStr());
    }

    @PostMapping
    public String cryptMessage(
            @RequestBody Message<String> message
    ) throws AesException {
        return BizMsgCryptUtil.getDecryptMsgMessage(bizMsgCrypt, message);
    }

}
